package com.rankeiot.dev.mode;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.rankeiot.core.util.JSONUtil;
import org.sagacity.sqltoy.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Table {


    private String title;
    private String code;
    @JsonIgnore
    private String packageName;
//    @JSONField(serialize=false)
//    private String packagePath;
    private String module;
    private String name;
    private String type="TABLE";

    private List<Column> columns=new ArrayList<>();

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

//    public String getPackagePath() {
//        return packagePath;
//    }
//
//    public void setPackagePath(String packagePath) {
//        this.packagePath = packagePath;
//    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }

    @JsonIgnore
    public String getPriv(){
        return module+"."+StringUtil.firstToLowerCase(name);
    }

    @JsonIgnore
    public String getListFields(){
        List<String> fields=new ArrayList<>();
        for(Column c:columns){
            if(c.isPrimaryKey()||c.isVisibleInList()){
                fields.add(c.getCode());
            }
        }
        if(fields.size()==columns.size()){
            return "*";
        }
        return String.join(",",fields);
    }
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    //获取前端的验证规则
    @JsonIgnore
    public String getRules() {
        //JSON.class
        // Feature.AllowArbitraryCommas
        Map<String,List<String>> rules=new HashMap();
        for(Column c:columns){
            if(c.isVisibleInForm()){
                if(StringUtil.isNotBlank(c.getVerify())) {
                    if(c.getVerify().contains("IDCardNo")) {
                        List<String> fields=rules.get("idcard");
                        if(fields==null) {
                            fields=new ArrayList();
                            rules.put("idcard",fields);
                        }
                        fields.add(c.getCode());
                    }
                    if(c.getVerify().contains("CnMobile")) {
                        List<String> fields=rules.get("mobile");
                        if(fields==null) {
                            fields=new ArrayList();
                            rules.put("mobile",fields);
                        }
                        fields.add(c.getCode());
                    }
                    if(c.getVerify().contains("Email")) {
                        List<String> fields=rules.get("email");
                        if(fields==null) {
                            fields=new ArrayList();
                            rules.put("email",fields);
                        }
                        fields.add(c.getCode());
                    }
                    if(c.getVerify().contains("url")) {
                        List<String> fields=rules.get("url");
                        if(fields==null) {
                            fields=new ArrayList();
                            rules.put("url",fields);
                        }
                        fields.add(c.getCode());
                    }
                    if(c.getVerify().contains("tel")) {
                        List<String> fields=rules.get("tel");
                        if(fields==null) {
                            fields=new ArrayList();
                            rules.put("tel",fields);
                        }
                        fields.add(c.getCode());
                    }
                }
            }
        }
        StringBuilder sb=new StringBuilder();
        sb.append("{\n");
        for(Map.Entry<String, List<String>> e:rules.entrySet()) {
            sb.append("\t\t\t\t");
            sb.append(e.getKey());
            sb.append(":");
            try {
                sb.append(JSONUtil.getObjectMapper().configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(),true).writeValueAsString(e.getValue()));//JSON.toJSONString(e.getValue(), SerializerFeature.UseSingleQuotes));
            } catch (JsonProcessingException ex) {
                throw new RuntimeException(ex);
            }
            sb.append(",\n");
        }
        sb.append("\t\t\t}");
        return sb.toString();
    }
    public String getPks(){
        List<String> pks=new ArrayList<>();
        for(Column c:this.columns){
            if(c.isPrimaryKey()){
                pks.add(c.getField());
            }
        }
       return String.join(",",pks);
    }
    public String pkArgs(){
        List<String> pks=new ArrayList<>();
        for(Column c:this.columns){
            if(c.isPrimaryKey()){
                pks.add(c.getDataType()+" "+c.getField());
            }
        }
        return String.join(",",pks);
    }
    public boolean isMultiPk(){
        int pkCount=0;
        for(Column c:this.columns){
            if(c.isPrimaryKey()){
                pkCount++;
            }
        }
        return pkCount>1;
    }
    public Column firstPk(){
        for(Column c:this.columns){
            if(c.isPrimaryKey()){
               return c;
            }
        }
        return null;
    }
    public boolean hasSearch(){
        for(Column c:columns){
            if(c.isSearchCondition()){
                return true;
            }
        }
        return false;
    }
//    public Column firstPk(){
//        for(Column c:this.columns){
//            if(c.isPrimaryKey()){
//                return c;
//            }
//        }
//        return null;
//    }
}
